package cn.bctools.mail.config;

import cn.bctools.mail.service.IJamesUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

/**
 * @author: Mat
 * @project: jvs-mailbox
 * @package: cn.bctools.mail.config
 * @date: 2025-06-30 23:31:09
 * @file: SecurityConfig
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    IJamesUserService jamesUserService;
    @Autowired
    JwtUtil jwtUtil;

//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        // 让这些路径完全绕过Spring Security
//        web.ignoring().antMatchers("/swagger-ui.html", "/swagger-ui/**", "/v2/api-docs", "/webjars/**", "/swagger-resources/**", "/auth/**");
//    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
                .antMatchers("/", "/error", "/favicon.ico").permitAll()
                // 放行Swagger资源
                .antMatchers("/v2/api-docs", "/swagger-resources/**", "/swagger-ui/**", "/swagger-ui.html", "/webjars/**").permitAll()
                // 放行登录接口
                .antMatchers("/auth/**").permitAll()
                // 其他接口需要认证
                .anyRequest().authenticated().and().addFilterBefore(new JwtAuthenticationTokenFilter(jamesUserService, jwtUtil), UsernamePasswordAuthenticationFilter.class);
    }


//    @Bean
//    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
//        http
//                .authorizeRequests()
//                // 无需权限的公共资源
//                .antMatchers("/", "/error", "/favicon.ico").permitAll()
//
//                // Swagger资源 - 仅允许ADMIN角色访问
////                .antMatchers("/swagger-ui.html", "/swagger-ui/**", "/v2/api-docs", "/webjars/**", "/swagger-resources/**").hasRole("ADMIN")
//                .antMatchers("/swagger-ui.html", "/swagger-ui/**", "/v2/api-docs", "/webjars/**", "/swagger-resources/**").permitAll()
//
//                // API接口 - 需认证
////                .antMatchers("/api/**").authenticated()
//
//                // 其他路径需认证
//                .anyRequest().authenticated()
//                .and()
//
//                // 基于表单的登录
//                .formLogin()
//                .loginPage("/auth/**")
//                .permitAll()
//                .and()
//
//                // 登出配置
////                .logout()
////                .invalidateHttpSession(true)
////                .clearAuthentication(true)
////                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
////                .logoutSuccessUrl("/login?logout")
////                .permitAll()
////                .and()
//
//                // 禁用CSRF保护 - Swagger需要
//                .csrf()
//                .ignoringAntMatchers("/v2/api-docs", "/swagger-ui.html", "/swagger-resources/**");
//
//        return http.build();
//    }
}